package 牛客_面试必刷.Month07.day13;

import java.util.ArrayList;
import java.util.Arrays;

public class 括号生成 {
    ArrayList<String> list = new ArrayList<>();
    public ArrayList<String> generateParenthesis (int n) {
        // write code here
        backstacking("",0,0,n);
        return list;
    }

    public void backstacking(String s,int left,int right,int n){
        if(s.length() == n * 2){
            list.add(s);
            return;
        }

        if(left < n){
            backstacking(s + "(",left + 1,right,n);
        }
        if(left > right){
            backstacking(s + ")",left,right + 1,n);
        }
    }
}
